package sdk.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sdk.service.EmpManager;

import model.Dept;
import model.EduDegree;
import model.Education;
import model.Emp;
import model.JobTitle;
import model.SkillGrade;

import util.Page;

public class FindEmpServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int pageNo=1;
		int pageSize=2;
		String keycode="";
		int idkeycode=0;
		
		if (null != request.getParameter("serchtype")) {
			request.getSession().setAttribute("serchtype",request.getParameter("serchtype"));
		}
		
		if (null != request.getParameter("keycode")) {
			request.getSession().setAttribute("keycode",request.getParameter("keycode"));
			keycode = request.getParameter("keycode");
		} else {
			keycode = (String) request.getSession().getAttribute("keycode");
		}
		try{
			if(request.getParameter("pageNo")!=null)
			{
				pageNo=Integer.parseInt(request.getParameter("pageNo"));
			}
			if(request.getParameter("pageSize")!=null)
			{
				pageSize=Integer.parseInt(request.getParameter("pageSize"));
				
				request.getSession().setAttribute("pageSize", pageSize);
			}
			if(request.getSession().getAttribute("pageSize")!=null)
			{
				pageSize=(Integer) request.getSession().getAttribute("pageSize");
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		Page<Emp> page=null;
		if("deptno".equals(request.getSession().getAttribute("serchtype"))){
			
			page= new EmpManager().getEmpByDeptNo(pageNo,pageSize,idkeycode);
		}
		else if("degree".equals(request.getSession().getAttribute("serchtype")))
		{
			EduDegree ed = new EduDegree();
			ed.setId(idkeycode);
			ed.setName(keycode);
			page=new EmpManager().getEmpByEduDegree(pageNo, pageSize,ed);				
		}
		else if("edu".equals(request.getSession().getAttribute("serchtype")))
		{
			Education ec = new Education();
			ec.setId(idkeycode);
			ec.setName(keycode);
			page=new EmpManager().getEmpByEducation(pageNo, pageSize,ec);				
		}
		else if("name".equals(request.getSession().getAttribute("serchtype")))
		{
			page=(Page<Emp>) new EmpManager().getEmpByEmpName(keycode);				
		}
		else if("sex".equals(request.getSession().getAttribute("serchtype")))
		{
			page=(Page<Emp>) new EmpManager().getEmpBySex(pageNo, pageSize,keycode);				
		}
		else if("jobTitle".equals(request.getSession().getAttribute("serchtype")))
		{
			JobTitle jt = new JobTitle();
			jt.setId(idkeycode);
			jt.setName(keycode);
			page=new EmpManager().getEmpByJobTitle(pageNo, pageSize,jt);				
		}
		else if("skillgrade".equals(request.getSession().getAttribute("serchtype")))
		{
			SkillGrade sg = new SkillGrade();
			sg.setId(idkeycode);
			sg.setName(keycode);
			page=new EmpManager().getEmpBySkillGrade(pageNo, pageSize,sg);				
		}
		
		else if("empid".equals(request.getSession().getAttribute("serchtype")))
		{
			try{
			Page<Emp> p=new Page<Emp>(1,1);
			List<Emp> list=new ArrayList<Emp>();
			if(null!=new EmpManager().getById(Integer.parseInt(keycode)))
			{
				list.add(new EmpManager().getById(Integer.parseInt(keycode)));
				p.setList(list);
				p.setTotalCount(1);
			}					
			page=p;
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		else
		{			
			page=new EmpManager().getAllEmp(pageNo, pageSize);
		}
		request.setAttribute("page", page);
		request.getRequestDispatcher("/Train/TeacherManager.jsp").forward(request, response);
	}

}
